package com.helen.ui.newui;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helen.entity.TableEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2019/1/31
 */
public class ActiveActivity extends BaseActivity {
    @BindView(R.id.tv_check_in)
    TextView tvCheckIn;
    @BindView(R.id.tv_wallet)
    TextView tvWallet;
    @BindView(R.id.tv_team)
    TextView tvTeam;
    @BindView(R.id.rel_1)
    RelativeLayout rel1;
    @BindView(R.id.rel_2)
    RelativeLayout rel2;
    @BindView(R.id.rel_3)
    RelativeLayout rel3;
    @BindView(R.id.rel_4)
    RelativeLayout rel4;
    @BindView(R.id.rel_5)
    RelativeLayout rel5;
    @BindView(R.id.rel_6)
    RelativeLayout rel6;
    private Intent ti = null;
    private List<TableEntity> tableEntityList = null;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_acvive;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("赚取财富");
        tableListApi();
    }

    @Override
    protected void updateViews() {

    }

    private void tableListApi() {
        mMProgressDialog.show();
        EasyHttp.post(Constants.API_JSONAPI_SHOW_TABLE_LIST)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mMProgressDialog.dismiss();
                        MToast.makeTextShort(ActiveActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String s) {
                        MyLog.d("yang", "s===" + s);
                        mMProgressDialog.dismiss();
                        String data = PhoneUtils.checkResponseFlag2(ActiveActivity.this, s, TAG);
                        if (data != null) {
                            tableEntityList = new Gson().fromJson(data, new TypeToken<List<TableEntity>>() {
                            }.getType());


                        }
                    }
                });
    }

    @OnClick({R.id.tv_check_in, R.id.tv_wallet, R.id.tv_team, R.id.tv_recovery_car, R.id.rel_1, R.id.rel_2, R.id.rel_3, R.id.rel_4, R.id.rel_5, R.id.rel_6, R.id.rel_7, R.id.rel_8})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_check_in:
                if (checkLoginState()) {
                    startActivity(new Intent(ActiveActivity.this, CheckInActivity.class));
                }
                break;
            case R.id.tv_wallet:
                if (checkLoginState()) {
                    startActivity(new Intent(ActiveActivity.this, WalletActivity.class));
                }
                break;
            case R.id.tv_team:
                if (checkLoginState()) {
                    startActivity(new Intent(ActiveActivity.this, TeamActivity.class));
                }
                break;
            case R.id.tv_recovery_car:
                if (checkLoginState()) {
                    startActivity(new Intent(ActiveActivity.this, MyRecoveryCarActivity.class));
                }
                break;
            case R.id.rel_1:
                gotoWebView(1);
                break;
            case R.id.rel_2:
                gotoWebView(2);
                break;
            case R.id.rel_3:
                gotoWebView(3);
                break;
            case R.id.rel_4:
                gotoWebView(4);
                break;
            case R.id.rel_5:
                gotoWebView(5);
                break;
            case R.id.rel_6:
                gotoWebView(6);
                break;
            case R.id.rel_7:
                gotoWebView(7);
                break;
            case R.id.rel_8:
                startActivity(new Intent(ActiveActivity.this, CustomerActivity.class));
                break;
        }
    }

    private void gotoWebView(int type) {
        String url = "";
        String title = "";
        if (tableEntityList != null && tableEntityList.size() > 0) {
            for (int i = 0; i < tableEntityList.size(); i++) {
                if (tableEntityList.get(i).getId() == type) {
                    url = tableEntityList.get(i).getUrl();
                    title = tableEntityList.get(i).getTitle();
                    break;
                }
            }
            ti = new Intent(ActiveActivity.this, RoyaltyWebViewActivity.class);
            ti.putExtra("title", title);
            ti.putExtra("url", url);
            startActivity(ti);

        }
    }
}
